var icon = require("../../images/icon.js");
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
var zhdomain = app.globalData.zhdomain;
Page({
    data:{
        zan:"",
        comment:"",
        loadingimg:"",
        title:"",
        id:0,
        detail:[], //内容详情
        extra:{}, //额外信息 评论+点赞
        longcomments:[], //长评论
        shortcomments:[], //短评论
        loading:true,
        loadlongcomment:false, //是否加载了长评论
        loadshortcomment:false //是否加载了短评论
    },
    onLoad:function(req){
        var that = this;
        wx.request({
            url: zhdomain+'/api/4/news/' + req.id,
            headers: {
                'Content-Type': 'application/json'
            },
            success (res) {
                WxParse.wxParse('article', 'html', res.data.body, that,0);
                that.setData({
                    title: res.data.title,
                    detail: res.data,
                    zan:icon.zan,
                    comment:icon.comment,
                    loadingimg:icon.loading,
                    id:req.id
                })
            },
            complete:function(res){
                if(res.statusCode != 200){
                    wx.showModal({
                        content: '加载失败请重试[errorCode:'+res.statusCode+']',
                        showCancel: false
                    });
                }
            }
        })
        //请求额外信息 评论+点赞
        wx.request({
            url: zhdomain+'/api/4/story-extra/' + req.id,
            headers: {
                'Content-Type': 'application/json'
            },
            success (res) {
                that.setData({
                    extra: res.data
                })
            },
            complete:function(res){
                if(res.statusCode != 200){
                    wx.showModal({
                        content: '加载失败请重试[errorCode:'+res.statusCode+']',
                        showCancel: false
                    });
                }
            }
        })
    },
    onReachBottom:function(){
        var that = this;
        //长评论
        if(!that.data.loadlongcomment){
            wx.request({
                url: zhdomain+'/api/4/story/' + that.data.id + "/long-comments",
                headers: {
                    'Content-Type': 'application/json'
                },
                success (res) {
                    let comment = res.data.comments;
                    for(var c in comment){
                        comment[c].time = new Date(parseInt(comment[c].time) * 1000).format('MM-DD hh:mm')
                    }
                    that.setData({
                        longcomments: comment
                    })
                },
                complete:function(res){
                    if(res.statusCode != 200){
                        wx.showModal({
                            content: '加载失败请重试[errorCode:'+res.statusCode+']',
                            showCancel: false
                        });
                    }else{
                        that.setData({
                            loading:false,
                            loadlongcomment:true
                        })
                    }
                }
            })
        }
        //短评论
        if(!that.data.loadshortcomment){
            wx.request({
                url: zhdomain+'/api/4/story/' + that.data.id + "/short-comments",
                headers: {
                    'Content-Type': 'application/json'
                },
                success (res) {
                    let comment = res.data.comments;
                    for(var c in comment){
                        comment[c].time = new Date(parseInt(comment[c].time) * 1000).format('MM-DD hh:mm')
                    }
                    that.setData({
                        shortcomments: comment
                    })
                },
                complete:function(res){
                    if(res.statusCode != 200){
                        wx.showModal({
                            content: '加载失败请重试[errorCode:'+res.statusCode+']',
                            showCancel: false
                        });
                    }else{
                        that.setData({
                            loading:false,
                            loadshortcomment:true
                        })
                    }
                }
            })
        }
    }
})